package cn.edu.hunau.java.week01.day04;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

public class InserSortTest {

    @Test
    public void testinsertSort(){
        int[] arr=randomArray(20,100);
        System.out.println("排序前:"+ Arrays.toString(arr));
        inserSort(arr);
        System.out.println("排序后"+Arrays.toString(arr));
    }
    @Test
    public void testSystem(){
        int[] src={1,2,3,4,5,6,7};
        int[] target=new int[10];
        System.arraycopy(src,0,target,2,5);
        System.out.println(Arrays.toString(target));
    }

    public void inserSort(int[] arr){
        for(int i=1;i<arr.length;i++){
            int temp=arr[i];
            int j=i-1;
            for(;j>0;j--){
                if(temp<arr[j]){
                    arr[j+1]=arr[j];
                    System.out.println(Arrays.toString(arr));
                }else{
                    break;
                }
            }
            arr[j]=temp;
        }
    }
    public int[] randomArray(int length,int bound){
        int [] arr=new int[length];
        Random r=new Random();
        for(int i=0;i<arr.length;i++){
            int temp=r.nextInt(bound)+1;
            boolean flag=true;
            for(int j=i-1;j>=0;j--){
                if(arr[j]==temp){
                    i--;
                    flag=false;
                    break;
                }
            }
            if(flag){
                arr[i]=temp;
            }
        }
        return arr;
    }
}
